package net.findkrypton.event;

import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import net.findkrypton.GameInfo;
import net.findkrypton.entity.ArrayBase;
import net.findkrypton.entity.ImageState;
import net.findkrypton.po.SaveFile;
import net.findkrypton.entity.BoxTimeBase;
import net.jumpengine.Manager;
import net.jumpengine.bean.Base;
import net.jumpengine.bean.ImageButton;
import net.jumpengine.bean.LEDBox;
import net.jumpengine.inter.IAction;
import net.jumpengine.inter.IStateEvent;
import net.jumpengine.manager.ImageManager;
import net.jumpengine.po.Border;
import net.jumpengine.util.UIO;

public class RunningStateEvent implements IStateEvent {
	private boolean load=false;
	private GameInfo info;
	private ArrayBase array;
	private ImageState imageState;
	private BoxTimeBase time;
	private LEDBox number;
	private ImageButton icon;
	
	private SaveFile save;
	private int[] ints;

	@Override
	public boolean isLoaded() {
		return load;
	}

	@Override
	public void load() {
		Manager.getLog().debug("RunningStateEvent[load]");
		
		ImageManager im = Manager.getImageManager();
		
		Border border=new Border(2, 2, 2, 2, 2);
		border.setColor(Color.YELLOW);
		
		LEDBox timeBox=new LEDBox(380, 10, 61, 3, border);
		timeBox.setColor(Color.web("#C82800"));
		im.add(timeBox);
		
		time=new BoxTimeBase(1000, timeBox);
		im.add(time);
		
		number=new LEDBox(740, 10, 61, 3, border);
		number.setColor(Color.web("#C82800"));
		number.setVal(info.getKcount());
		im.add(number);
		
		array=new ArrayBase(info,UIO.loadImage("/run/box.png",this.getClass())
				,UIO.loadImage("/run/krypton.png",this.getClass()),UIO.loadImage("/run/flag.png",this.getClass()));
		array.setEvent(this);
		im.add(array);
		
		Image[] images=new Image[] {
				UIO.loadImage("/run/nomal.png",this.getClass()),
				UIO.loadImage("/run/down.png",this.getClass()),
				UIO.loadImage("/run/loser.png",this.getClass()),
		};
		imageState=new ImageState(570, 10, 81, 61, images);
		imageState.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				array.reset();
				time.reset();
				number.setVal(info.getKcount());
			}
		});
		im.add(imageState);
		
		icon=new ImageButton(980, 10, UIO.loadImage("/run/icon.png",this.getClass()));
		icon.setVisible(false);
		icon.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				array.back();
			}
		});
		im.add(icon);
		
		load=true;
	}
	
	@Override
	public void show() {
		if(save!=null) {
			array.reload(save, ints);
			save=null;
			ints=null;
		}
	}

	@Override
	public void pause() { }

	@Override
	public void end() { }
	
	public void setReLoad(SaveFile save,int[] ints) {
		this.save=save;
		this.ints=ints;
	}

	public GameInfo getInfo() {
		return info;
	}
	public void setInfo(GameInfo info) {
		this.info = info;
	}

	public ArrayBase getArray() {
		return array;
	}

	public ImageState getImageState() {
		return imageState;
	}

	public BoxTimeBase getTime() {
		return time;
	}

	public LEDBox getNumber() {
		return number;
	}

	public ImageButton getIcon() {
		return icon;
	}

	@Override
	public void setBackImage(Image image) { }
}
